package routers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	."gitee.com/edgebox/metadata/internal/model"
)

func AddKeyPair(c *gin.Context){
	var json KeyPair
	err :=c.BindJSON(&json)
	if err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}
	if err:=json.Insert();err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}else{
		c.JSON(http.StatusOK,reply{
			Code:0,
			Msg:"success",
			Data:nil,
		})
		return
	}
}
func GetKeyPairs(c *gin.Context){
	mods := FindKeyPairs(&KeyPair{})
	if mods != nil && len(mods) > 0{
		c.JSON(http.StatusOK,reply{
			Code:len(mods),
			Msg:"success",
			Data:mods,
		})
		return
	}else{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:"not found",
			Data:nil,
		})
		return
	}
}
func GetKeyPairById(c *gin.Context){
	id:= c.Param("id")
	mods := FindKeyPairs(&KeyPair{Id:id})
	if mods != nil && len(mods) > 0{
		c.JSON(http.StatusOK,reply{
			Code:1,
			Msg:"success",
			Data:mods[0],
		})
		return
	}else{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:"not found",
			Data:nil,
		})
		return
	}
}
func UpdateKeyPair(c *gin.Context) {
	var json KeyPair
	err :=c.BindJSON(&json)
	if err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}
	if num,err:=json.Save();err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}else{
		c.JSON(http.StatusOK,reply{
			Code:int(num),
			Msg:"success",
			Data:nil,
		})
		return
	}
}
func DelKeyPair(c *gin.Context) {
	id:= c.Param("id")
	var json KeyPair
	json.Id = id
	if num,err:=json.Delete();err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}else{
		c.JSON(http.StatusOK,reply{
			Code:int(num),
			Msg:"success",
			Data:nil,
		})
		return
	}
}